package com.dap.system.office.service.impl;

import java.util.List;

import com.dap.common.core.exception.CustomException;
import com.dap.common.core.utils.DateUtils;
import com.dap.common.core.utils.SecurityUtils;
import com.dap.common.core.utils.StringUtils;
import com.dap.system.office.domain.TScore;
import com.dap.system.office.domain.vo.TCourseSelectorVo;
import com.dap.system.office.domain.vo.TStudentScoreInfoVo;
import com.dap.system.office.mapper.TScoreMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import com.dap.system.office.mapper.TCourseMapper;
import com.dap.system.office.domain.TCourse;
import com.dap.system.office.service.ITCourseService;

import javax.annotation.Resource;

/**
 * 课程Service业务层处理
 *
 * @author dap
 * @date 2021-11-10
 */
@Service
public class TCourseServiceImpl implements ITCourseService
{
    @Resource
    private TCourseMapper tCourseMapper;

    @Resource
    private TScoreMapper tScoreMapper;

    /**
     * 查询课程
     *
     * @param id 课程ID
     * @return 课程
     */
    @Override
    public TCourse selectTCourseById(Long id)
    {
        return tCourseMapper.selectTCourseById(id);
    }

    /**
     * 查询课程列表
     *
     * @param tCourse 课程
     * @return 课程
     */
    @Override
    public List<TCourse> selectTCourseList(TCourse tCourse)
    {
        return tCourseMapper.selectTCourseList(tCourse);
    }

    /**
     * 分页查询课程列表
     *
     * @param tCourse 课程
     * @return 课程
     */
    @Override
    public List<TCourse> selectTCourseListPage(TCourse tCourse)
    {
        PageHelper.clearPage();
        Page<Object> objects = PageHelper.startPage(tCourse.getPageNum(), tCourse.getPageSize());
        objects.getTotal();
        PageHelper.clearPage();
        List<TCourse> list = tCourseMapper.selectTCourseList(tCourse);
        PageInfo<TCourse> tPageInfo = new PageInfo<>(list);
        List<TCourse> dataList = tPageInfo.getList();
        return dataList;
    }
    /**
     * 新增课程
     *
     * @param tCourse 课程
     * @return 结果
     */
    @Override
    public int insertTCourse(TCourse tCourse)
    {
        List<TCourse> tCourses=tCourseMapper.selectTCourseList(tCourse);
        if (tCourses==null || tCourses.size()==0) {
            tCourse.setCreateBy(SecurityUtils.getUsername());
            tCourse.setCreateTime(DateUtils.getNowDate());
            tCourse.setDelFlag("0");
            return tCourseMapper.insertTCourse(tCourse);
        }
        return 0;
    }

    /**
     * 修改课程
     *
     * @param tCourse 课程
     * @return 结果
     */
    @Override
    public int updateTCourse(TCourse tCourse)
    {
        TCourse course=new TCourse();
        course.setCouName(tCourse.getCouName());
        List<TCourse> tCourseSelectorVos=tCourseMapper.selectTCourseList(course);
        if (tCourseSelectorVos.size()>0){
            throw new CustomException("该课程已存在，修改失败");
        }
        tCourse.setUpdateTime(DateUtils.getNowDate());
        tCourse.setUpdateBy(SecurityUtils.getUsername());
        tCourse.setDelFlag("0");
        return tCourseMapper.updateTCourse(tCourse);
    }

    /**
     * 批量删除课程
     *
     * @param ids 需要删除的课程ID
     * @return 结果
     */
    @Override
    public int deleteTCourseByIds(Long[] ids)
    {
        for (Long id : ids){
            TScore score = new TScore();
            score.setCourseId(id);
            TStudentScoreInfoVo<Object> tStudentScoreInfoVo = new TStudentScoreInfoVo<>();
            List<TScore> tScores = tScoreMapper.selectTScoreList(score);
            if(tScores.size() > 0){
                throw new CustomException("该课程已被学生选择，禁止删除");
            }
        }
        return tCourseMapper.deleteTCourseByIds(ids);
    }

    /**
     * 删除课程信息
     *
     * @param id 课程ID
     * @return 结果
     */
    @Override
    public int deleteTCourseById(Long id)
    {
        return tCourseMapper.deleteTCourseById(id);
    }

    @Override
    public List<TCourseSelectorVo> selectTCourseNameList() {
        return tCourseMapper.selectTCourseNameList();
    }

    @Override
    public String importCourse(List<TCourse> courseList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(courseList) || courseList.size() == 0)
        {
            throw new CustomException("导入课程数据不能为空！");
        }else{
            for (TCourse courses : courseList){
                TCourse course=tCourseMapper.selectCourseName(courses.getId());
                if (course !=null){
                    courses.setId(course.getId());
                    courses.setTeaName(course.getTeaName());
                    courses.setUpdateBy(SecurityUtils.getUsername());
                    courses.setUpdateTime(DateUtils.getNowDate());
                    courses.setDelFlag("0");
                    tCourseMapper.updateTCourse(courses);
                }else {
                    courses.setTeaName(course.getTeaName());
                    courses.setCreateTime(DateUtils.getNowDate());
                    courses.setCreateBy(SecurityUtils.getUsername());
                    courses.setDelFlag("0");
                    tCourseMapper.insertTCourse(courses);
                }
            }

            return "导入成功";
        }
    }
}
